package com.netx.data;
import java.util.List;
import com.netx.generics.collections.ImmutableList;
import com.netx.generics.util.Tools;
import com.netx.generics.basic.IntegrityException;


public final class EntityMetaData extends MetaData {

	private final boolean _autoGenerateKeys;
	private final String _className;
	private final ImmutableList<DataSink> _replication;
	private RelationMetaData _relation;
	
	// For DatabaseAnalyzer:
	EntityMetaData(String name, String className, List<DataSink> replication, List<String[]> constraints, boolean autoGenerateKeys, Database database) {
		super(name, constraints, database);
		_className = className;
		_replication = new ImmutableList<DataSink>(replication);
		_autoGenerateKeys = autoGenerateKeys;
		_relation = null;
	}

	public boolean autoGeneratedKeys() {
		return _autoGenerateKeys;
	}

	public RelationMetaData getRelationMetaData() {
		return _relation;
	}
	
	// For DatabaseAnalyzer:
	void setRelation(RelationMetaData relation) {
		this._relation = relation;
	}

	// For Connection:
	ImmutableList<DataSink> getReplicationSinks() {
		return _replication;
	}

	// For Connection:
	Entity createEntity(Connection c) {
		try {
			Class<?> oClass = Class.forName(_className);
			Entity e = (Entity)Tools.createObject(oClass);
			e.init(this, c);
			return e;
		}
		catch(ClassNotFoundException cnfe) {
			// TODO treat this exception
			throw new IntegrityException(cnfe);
		}
	}
}
